package com.zw.pdm.module.craft.controller;

import com.zw.pdm.base.BaseController;
import com.zw.pdm.common.JsonResult;
import com.zw.pdm.entity.craft.CraMakeDetail;
import com.zw.pdm.entity.craft.CraMakeSheet;
import com.zw.pdm.entity.production.DrawChange;
import com.zw.pdm.entity.production.MakeChange;
import com.zw.pdm.entity.production.MakePlan;
import com.zw.pdm.entity.production.MakeSheet;
import com.zw.pdm.module.basic.service.DictCommonService;
import com.zw.pdm.module.craft.model.CraProQueryModel;
import com.zw.pdm.module.craft.model.ServiceTaskModel;
import com.zw.pdm.module.craft.repository.CraftMakeDetailRepository;
import com.zw.pdm.module.craft.service.CraftMaintainService;
import com.zw.pdm.module.craft.service.CraftProQueryService;
import com.zw.pdm.module.craft.service.CraftReceiveService;
import com.zw.pdm.module.craft.service.ProductionInputService;
import com.zw.pdm.module.report.model.Project;
import com.zw.pdm.module.report.service.SheetService;
import com.zw.pdm.module.system.service.DeptService;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.zw.pdm.constant.Constant.GROUP_SPECTYPE;

/**
 * @Description: 计划查询接口
 * @Author: China.sgl
 * @Date: 2020/8/3 16:11
 */
@Controller
@RequestMapping("/craft/productionInput")
@SuppressWarnings("all")
public class ProductionInputController extends BaseController {

    @Autowired
    private ProductionInputService productionInputService;
    @Autowired
    private DictCommonService dictCommonService;
    @Autowired
    private SheetService sheetService;
    @Autowired
    private DeptService deptService;
    @Autowired
    private CraftMaintainService craftMaintainService;

    @GetMapping
    @RequiresPermissions({"tec:proProduc:view"})
    public String page(Model model) {
        model.addAttribute("specialId", dictCommonService.findDictByGroupName(GROUP_SPECTYPE));
//        model.addAttribute("depts", deptService.findAllTopLevelDept());
        return "craft/productionInput/page";
    }
    /**
     * 跳转到导入界面
     */
    @GetMapping("/tiaozhuan/{sheetId}")
    @RequiresPermissions({"tec:proProduc:edit"})
    public String tiaozhuan(@PathVariable("sheetId") Integer sheetId,Model model) {
        CraMakeSheet craMakeSheet = craftMaintainService.selectCraftMakeSheetModel(sheetId);
        model.addAttribute("craMakeSheet", craMakeSheet);
        return "craft/productionInput/inputExPage";
    }

    /**
     * 列表查询
     */
    @ResponseBody
    @PostMapping("/list")
//    @RequiresPermissions({"report:query:view"})
    public JsonResult list(CraMakeSheet craMakeSheet) {
        startPage();
        List<CraMakeSheet> list = productionInputService.findWithCraftProduction(craMakeSheet);
        return jsonResult(list);
    }

    /**
     * 增加总图号
     *
     * @param craMakeDetail
     * @return
     */
    @ResponseBody
    @PostMapping("/add/Production")
    @RequiresPermissions({"tec:proProduc:edit"})
    public JsonResult addProduction(CraMakeSheet craMakeSheet) {
        List<CraMakeSheet> craMakeSheetList = productionInputService.selectProByProCode(craMakeSheet);
        if (craMakeSheetList.size() > 0) {

            return JsonResult.success("已经创建过此项目信息！！", -1);
        }
        productionInputService.addCraMakeSheet(craMakeSheet);
        return jsonResult(1);
    }

    /**
     * 修改总图号
     *
     * @param craMakeDetail
     * @return
     */
    @ResponseBody
    @PostMapping("/edit/Production")
    @RequiresPermissions({"tec:proProduc:edit"})
    public JsonResult editProduction(CraMakeSheet craMakeSheet) {

        productionInputService.updateCraMakeSheet(craMakeSheet);
        return jsonResult(1);
    }
    /**
     * 删除总图号
     *
     * @param craMakeDetail
     * @return
     */
    @ResponseBody
    @PostMapping("/del")
    @RequiresPermissions({"tec:proProduc:del"})
    public JsonResult del( Integer id) {

        List<CraMakeDetail> craMakeDetailList = productionInputService.selectCraMakeDetailBySheetId(id);
        if ( craMakeDetailList.size()>0   ){
            return JsonResult.success("请先删除此部分下的所有制作件清单再删除本项！！", -1);
        }
        return jsonResult( productionInputService.updateCraMakeSheet(id));
    }
    /**查询非设计项目
     * @param q
     * @return
     */
    @GetMapping("/project/list")
    @ResponseBody
//    @RequiresPermissions(value = {"production:make:edit"}, logical = Logical.OR)
    public List<Project> proList(String q) {
        return productionInputService.findProjectByCode(q);
    }
    /**
     * 查看操作
     */
    @GetMapping("/info/{sheetId}")
//    @RequiresPermissions({"report:query:view"})
    public String info(@PathVariable("sheetId") Integer sheetId, Model model) {
        CraMakeSheet craMakeSheet = craftMaintainService.selectCraftMakeSheetModel(sheetId);
        model.addAttribute("craMakeSheet", craMakeSheet);
        return "craft/productionInput/info";
    }
    /**
     * 列表明细
     */
    @ResponseBody
    @PostMapping("/detail")
//    @RequiresPermissions({"report:query:view"})
    public JsonResult detailList(Integer sheetId) {
        List<CraMakeDetail> craMakeDetailList = craftMaintainService.selectCraftMakeDetail(sheetId);
        return jsonResult(craMakeDetailList);
    }
    /**
     * 保存制作件
     */
    @ResponseBody
    @PostMapping("/save")
    @RequiresPermissions({"tec:proProduc:edit"})
    public JsonResult saveMake(Integer sheetId, String[] drawNo, Integer[] drawNum, String[] drawName,String[] makeType, String[] remark, String[] url,Integer[] orderNum) {
        int n = productionInputService.saveMakeDetail(sheetId, drawNo, drawNum, drawName, makeType, remark, url, orderNum);
        if(n<0){
            return JsonResult.success("保存失败！",-2);
        }
        return jsonResult(1);
    }
    /**
     * 保存制作件
     */
    @ResponseBody
    @PostMapping("/add")
    @RequiresPermissions({"tec:proProduc:edit"})
    public JsonResult addMake(CraMakeDetail craMakeDetail) {

        return jsonResult(productionInputService.saveMakeDetailOne(craMakeDetail));
    }
    /**
     * 修改保存
     * @param craMakeDetail
     * @return
     */
    @ResponseBody
    @PostMapping("/edit")
    @RequiresPermissions({"tec:proProduc:edit"})
    public JsonResult maintainCraMakeDetail(CraMakeDetail craMakeDetail) {

        return jsonResult(productionInputService.updateProductDetail(craMakeDetail));
    }

}
